package com.collect.zambia.zhiScene.zhiWidget.zhiNavigationBar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.collect.zambia.R;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("NonConstantResourceId")
public class ZHITopTabBar extends LinearLayout {

    private final Context mContext;

    @BindView(R.id.tbi_i0)
    ZHITopTabBarItem tbi_i0;
    @BindView(R.id.tbi_i1)
    ZHITopTabBarItem tbi_i1;

    private ZHITabBarItemClickListener mZHITabBarItemClickListener;

    public ZHITopTabBar(Context context) {
        this(context, null);
    }

    public ZHITopTabBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHITopTabBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        initView();
    }

    private void initView() {
        View mView = View.inflate(mContext, R.layout.view_top_tabbar, this);
        ButterKnife.bind(this);

        tbi_i0.setZHITabBarItemClickListener(index -> {
            tbi_i0.setChecked(true);
            tbi_i1.setChecked(false);

            if (mZHITabBarItemClickListener != null) {
                mZHITabBarItemClickListener.onTabClick(index);
            }
        });

        tbi_i1.setZHITabBarItemClickListener(index -> {
            tbi_i0.setChecked(false);
            tbi_i1.setChecked(true);

            if (mZHITabBarItemClickListener != null) {
                mZHITabBarItemClickListener.onTabClick(index);
            }
        });
    }

    public void setZHITabBarItemClickListener(ZHITabBarItemClickListener listener) {
        this.mZHITabBarItemClickListener = listener;
    }

    public int getCurIndex() {
        int cindex = 0;
        try {
            if (tbi_i0.isChecked()) {
                String gTag = (String) tbi_i0.getTag();
                if (!TextUtils.isDigitsOnly(gTag)) {
                    gTag = "0";
                }
                cindex = Integer.parseInt(gTag);

            } else if (tbi_i1.isChecked()) {
                String gTag = (String) tbi_i1.getTag();
                if (!TextUtils.isDigitsOnly(gTag)) {
                    gTag = "0";
                }
                cindex = Integer.parseInt(gTag);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cindex;
    }
}
